import axios from 'axios';
import type {
  ResponseInfo,
  CollectReq,
  CollectRepVo,
  SampleDataReq,
  SampleDataRepVo,
  FaceSearchParams,
  SearchResult, CompareResult, FaceDataReq
} from './types';

// 创建axios实例
const api = axios.create({
  baseURL: '/test',
  timeout: 60000
});

// 请求拦截器
api.interceptors.request.use(config => {
  // 可在此处添加token等全局参数
  return config;
});

// 响应拦截器
api.interceptors.response.use(
  response => {
    if (response.data?.code !== 0) {
      return Promise.reject(response.data);
    }
    return response;
  },
  error => {
    const message = error.response?.data?.message || '请求失败，请稍后重试';
    console.error(`API Error: ${message}`);
    return Promise.reject(error);
  }
);

// 集合管理API
export const collectApi = {
  create: (data: CollectReq) => api.post<ResponseInfo<boolean>>('/visual/collect/create', data),

  delete: (params: { namespace: string; collectionName: string }) =>
    api.get<ResponseInfo<boolean>>('/visual/collect/delete', { params }),

  list: (namespace: string) =>
    api.get<ResponseInfo<CollectRepVo[]>>('/visual/collect/list?namespace='+namespace),
};

// 样本管理API
export const sampleApi = {
  create: (data: SampleDataReq) => api.post<ResponseInfo<boolean>>('/visual/sample/create', data),

  delete: (params: { namespace: string; collectionName: string; sampleId: string }) =>
    api.get<ResponseInfo<boolean>>('/visual/sample/delete', { params }),

  get: (params: { namespace: string; collectionName: string; sampleId: string }) =>
    api.get<ResponseInfo<SampleDataRepVo>>('/visual/sample/get', { params }),

  list: (params: {
    namespace: string;
    collectionName: string;
    offset?: number;
    limit?: number;
    order?: 'asc' | 'desc';
  }) => api.get<ResponseInfo<SampleDataRepVo[]>>('/visual/sample/list', { params }),

  update: (data: SampleDataReq) => api.post<ResponseInfo<boolean>>('/visual/sample/update', data),
};

export const faceApi= {
  create: (data: FaceDataReq)=>api.post("/visual/face/create", data)
}

// 人脸搜索API
export const searchApi = {
  faceSearch: (data: FaceSearchParams) =>
    api.post<ResponseInfo<SearchResult[]>>('/visual/search/do', data),

  faceCompare: (data: { imageBase64A: string; imageBase64B: string }) =>
    api.post<ResponseInfo<CompareResult>>('/visual/compare/do', data),
};

// 导出所有API
export default {
  collectApi,
  sampleApi,
  searchApi,
};
